package cn.tedu.tmall.admin.mall.service.impl;

import cn.tedu.tmall.admin.mall.dao.persist.repository.IGoodsRepository;
import cn.tedu.tmall.admin.mall.dao.search.GoodsElasticsearchRepository;
import cn.tedu.tmall.admin.mall.service.IGoodsService;
import cn.tedu.tmall.common.pojo.po.GoodsSearchPO;
import cn.tedu.tmall.common.pojo.vo.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GoodsServiceImpl implements IGoodsService {

    @Autowired
    private IGoodsRepository goodsRepository;
    @Autowired
    private GoodsElasticsearchRepository goodsElasticsearchRepository;

    @Override
    public void rebuildSearch() {
        goodsElasticsearchRepository.deleteAll();

        Integer pageNum = 1;
        Integer pageSize = 3;
        Integer maxPage;
        do {
            PageData<GoodsSearchPO> pageData = goodsRepository.listSearch(pageNum, pageSize);
            maxPage = pageData.getMaxPage();
            goodsElasticsearchRepository.saveAll(pageData.getList());
            pageNum++;
        } while (pageNum <= maxPage);
    }

}
